package com.yj.provide.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.yj.pojo.Payment;
import com.yj.pojo.Result;
import com.yj.provide.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
    @Autowired
    PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/create")
    public Result create(@RequestBody Payment payment) {
        int i = paymentService.create(payment);
        log.info(i + "条插入成功");
        if (i > 0) {
            return new Result(200, "插入成功" + serverPort, i);
        } else {
            return new Result(444, "插入失败" + serverPort);
        }
    }

    @GetMapping("/get/{id}")
    public Result queryById(@PathVariable("id") Long id) {
        Payment payment = paymentService.queryById(id);
        log.info("查询成功" + payment);
        if (payment != null) {
            return new Result(200, "查询成功" + serverPort, payment);
        } else {
            return new Result(444, "查询失败" + serverPort);
        }
    }

    @GetMapping("/timeout")
    public String paymentTimeOut() throws InterruptedException {
        TimeUnit.SECONDS.sleep(2);
        return serverPort;
    }

    @GetMapping("/error")
    public String paymentError() throws InterruptedException {
        int a = 1 / 0;//抛出异常
        return serverPort;
    }

    /**
     * 资源限流或熔断后默认返回：Blocked by Sentinel (flow limiting)
     * @SentinelResource 配置sentinel资源，热点key限流规则只能在注解指定的资源上添加
     * 参数说明
     * value：资源名称
     * blockHandler指定方法在限流或熔断后会调用，如果未指定会报java.lang.reflect.UndeclaredThrowableException
     * blockHandlerClass：blockHandler方法默认在本类中定义，如果定义在其他类，必须是static方法，用该属性指定类
     * fallback指定方法在异常降级后会调用，如果未指定会抛出原异常堆栈
     * fallbackClass：fallback方法默认在本类中定义，如果定义在其他类，必须是static方法，用该属性指定类
     * exceptionsToIgnore排除异常列表，不计入异常统计，也不会调用fallback方法，抛出原异常堆栈
     */
    @GetMapping("/hotKey")
    @SentinelResource(value = "hotKey", blockHandler = "blockHandler", fallback = "fallback")
    public String hotKey(@RequestParam(value = "p1", required = false) String p1,
                         @RequestParam(value = "p2", required = false) Integer p2) {
        if (p2 != null) {
            int a = 1 / p2;//p2传入0时抛出异常
        }
        return "hotKey调用正常！";
    }

    /**
     * 限流或熔断后的处理方法，非static，方法签名必须与原方法一致，参数列表多一个BlockException
     */
    public String blockHandler(String p1, Integer p2, BlockException e) {
        return "hotKey已限流或熔断：" + e.getMessage();
    }

    /**
     * 异常降级处理方法，非static，方法签名必须与原方法一致，参数列表多一个Throwable
     */
    public String fallback(String p1, Integer p2, Throwable e) {
        return "hotKey调用异常：" + e.getMessage();
    }
}
